//
//  ZConsole.m
//  Pods
//
//  Created by 刘真 on 16/02/2017.
//
//

#import "ZConsole.h"
#import "ConsoleViewController.h"
#import "StatusViewController.h"

const CGRect consoleFrame = {6, 20, 100, 10};

@implementation ZConsole

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    static id shared = nil;
    dispatch_once(&onceToken, ^{
        shared = [[[self class] alloc] initWithFrame:consoleFrame];
    });
    return shared;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.windowLevel = UIWindowLevelStatusBar + 1;
        self.rootViewController = [[StatusViewController alloc] init];
        self.clipsToBounds = YES;
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(didChangeStatusBarOrientation:)
                                                     name:UIApplicationDidChangeStatusBarOrientationNotification
                                                   object:nil];
    }
    return self;
}

- (void)didChangeStatusBarOrientation:(NSNotification *)aNotification {
    self.frame = consoleFrame;
}

+ (void)show {
    UIWindow *shared = [[self class] shared];
    shared.hidden = NO;
}

+ (void)log:(nullable NSString *)log {
    StatusViewController *status = (StatusViewController *)[[[self class] shared] rootViewController];
    [status log:log];
}

@end
